package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.chargePayment.entity.req.SmsWnsChargePaymentRecordReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.LoanBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsStudentLoanAmountReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.StudentLoanAmountExportReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsStudentLoanAmountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 学生助学贷款金额表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-03-24
 */
@RestController
@SecretBody
@RequestMapping("/welcomeNewStudent/studentLoanAmount")
public class SmsWnsStudentLoanAmountController {

    @Resource
    private SmsWnsStudentLoanAmountService smsWnsStudentLoanAmountService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("分配助学贷款金额")
    @PostMapping("/divide")
    public ResponseResult divide(@RequestBody SmsWnsChargePaymentRecordReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanAmountService.divideStudentLoanAmount(req, userId);
    }

    @ApiOperation("查询学生本人助学贷款余额及分配情况")
    @GetMapping("/ownDetail")
    public ResponseResult ownDetail(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanAmountService.ownDetail(userId);
    }

    @ApiOperation("学生助学贷款余额详情")
    @GetMapping("/detail")
    public ResponseResult detail(@RequestParam("id") String id) {
        return smsWnsStudentLoanAmountService.detail(id);
    }

    @ApiOperation("查询学生助学贷款余额列表")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "orgId", required = false) Integer orgId,
                               @RequestParam(value = "searchStr", required = false) String searchStr) {
        return smsWnsStudentLoanAmountService.list(pageNum, pageSize, orgId, searchStr);
    }

    @ApiOperation("学生助学金使用情况导出")
    @PostMapping("/batch/exportLoanAmountList")
    public ResponseResult exportLoanAmountList(@RequestBody StudentLoanAmountExportReq exportReq, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsStudentLoanAmountService.exportLoanAmountList(exportReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("修改助学贷款余额")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody SmsWnsStudentLoanAmountReq req) {
        return smsWnsStudentLoanAmountService.update(req);
    }

    @ApiOperation(value = "学生助学贷款核销金额批量导入")
    @PostMapping("/batch/loanAdd")
    public ResponseResult loanBatchAdd(@RequestBody LoanBatchAddReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsWnsStudentLoanAmountService.feesRecordBatchAdd(req, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

}

